<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.hnas.core.util.PropertyUtil"%>
<%@page import="com.hnas.core.dto.DataBean"%>
<%
//boolean incDisplayTable = (null == request.getParameter("incDisplayTable") || !"false".equals(request.getParameter("incDisplayTable")));
//boolean incEditTable = (null == request.getParameter("incEditTable") || !"false".equals(request.getParameter("incEditTable")));
//boolean incUploadDiv = (null == request.getParameter("incUploadDiv") || !"false".equals(request.getParameter("incUploadDiv")));
//boolean incBatchDelete = (null == request.getParameter("incBatchDelete") || !"false".equals(request.getParameter("incBatchDelete")));
//boolean incDisplayStatus = (null == request.getParameter("incDisplayStatus") || !"false".equals(request.getParameter("incDisplayStatus")));
%>
<%if (incDisplayTable){%>
<div class="m_b_20">

<%if (incBatchDelete){%><form method="post" action="#" id="deleteForm" name="deleteForm"><%}%>
					<table class="tb_style05" width="100%" style="table-layout:fixed;">
						<thead>
							<tr>
	<%if (incBatchDelete){%><th><input type="checkbox" onclick="batchCheckMe(this,'ids');">全选</th><%}%>
								<c:forEach items="${beans}" var="o">
								 <c:if test="${o.display }"><th>${o.title}</th></c:if>
								</c:forEach>
	<%if (incDisplayStatus){%><th>状态</th><%}%>
							<th width="180">操作</th>
							</tr>
						</thead>
					<tbody>
					<c:forEach items="${pg.list}" var="o">
							<tr>
	<%if (incBatchDelete){%><td><input type="checkbox" name="ids" value="${o.id}"></td><%}%>
						<c:forEach items="${beans}" var="o1">
						<c:if test="${o1.display}">
						<td style="word-wrap:break-word;"><%=PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())%></td></c:if>
						</c:forEach>   
	<%if (incDisplayStatus){%><td>${o.valid ?'有效':'无效'}</td><%}%>
								<td><a href="#" class="c_3366cc btn editData" id="${o.id }">编辑</a>&nbsp;&nbsp;<a href="#" class="c_3366cc btn deleteData" id="${o.id }">删除</a>
								<a href="#" class="c_3366cc btn addUserData" id="${o.id }">添加用户</a>
								<c:if test="${not empty incCopyAll &&  incCopyAll =='true'}">&nbsp;&nbsp;<a href="#" class="c_3366cc btn copyData" id="${o.id }">复制增加</a></c:if></td>
							</tr>
					</c:forEach>
				</tbody>
			  </table>
<%if (incBatchDelete){%></form><%}%>
			  	</div>
				<div class="searpage" align="right">
					<div>
						${pg.pageStr}
					</div>
				</div>
				<br/>
<%}%>
<%if (incEditTable){%>
<!-- 添加start -->
<div class="editDataDiv" id="editDIV" title="${pageTitle}" style="display: none;" >
	<form action="#" id="editFm" method="post" name="editFm" class="editFm" >
	<input type="hidden" name="id" id="dealagencyid" value="0">
		<table >
			<c:forEach items="${beans}" var="o">
			<c:choose>
			<c:when test="${o.hidden}">
			<input type="hidden" name="${o.field}" id="deal${o.fieldId}" value="0">
			</c:when>
			<c:otherwise>
			<tr>
 				<td width="25%" align="right"> 
 					<label>${o.title}：</label>  
 				</td>
				<td> 
					<input type="text" name="${o.field}" id="deal${o.fieldId}" style="width:${o.length gt 0?o.length:200}px"  <c:if test="${o.maxlength>0}">maxlength="${o.maxlength}"</c:if>/>
					<c:if test="${not empty o.hint}">（${o.hint}）</c:if>
					<c:if test="${empty o.hint && o.dataType=='boolean'}">（1--是；0--否）</c:if>
					<c:if test="${!o.nullable}"><font color="red">*</font></c:if>
				</td>
  			</tr>
			</c:otherwise>
			</c:choose>			
			</c:forEach>
		
			<tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr>
		</table>
	</form>
</div>
<!-- 添加end -->
<%}%>
<%if (incEditUserTable){%>
<!-- 添加start -->
<div class="editDataDiv" id="editUserDIV" title="${pageTitle}" style="display: none;" >
	<form action="#" id="editUserFm" method="post" name="editUserFm" class="editFm" >
	<input type="hidden" name="id" id="dealid" value="0">
	<table >
			<c:forEach items="${beans1}" var="o">
			<c:choose>
			<c:when test="${o.hidden}">
			<input type="hidden" name="${o.field}" id="deal${o.fieldId}" value="0">
			</c:when>
			<c:otherwise>
			<tr>
 				<td width="25%" align="right"> 
 					<label>${o.title}：</label>  
 				</td>
				<td> 
					<input type="text" name="${o.field}" id="deal${o.fieldId}" style="width:${o.length gt 0?o.length:200}px"  <c:if test="${o.maxlength>0}">maxlength="${o.maxlength}"</c:if>/>
					<c:if test="${not empty o.hint}">（${o.hint}）</c:if>
					<c:if test="${empty o.hint && o.dataType=='boolean'}">（1--是；0--否）</c:if>
					<c:if test="${!o.nullable}"><font color="red">*</font></c:if>
				</td>
  			</tr>
			</c:otherwise>
			</c:choose>			
			</c:forEach>
		
			<tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr>
		</table>
	</form>
</div>
<!-- 添加end -->
<%}%>
<%if (incUploadDiv){%>
<!-- 文件导入start -->
<div class="ImportDataDiv" id="ImportDataDiv" title="导入" style="display: none;" >
	<form action="#" id="Importfm" method="post" enctype="multipart/form-data"  name="Importfm">
		<table >
			<tr> 
				<td>
					<a class="a_btn" href="${pageContext.request.contextPath }/templete/<%=uploadTmp%>"><span>下载模板</span></a>
				</td>
			</tr>
		</table>
		<table > 
			<tr>
 				<td width="25%"> 
 					<label>导入数据：</label>  
 				</td>
				<td> 
					<input type="file" name="formFile" id="formFile"/> 
				</td>
  			</tr>
		</table>
	</form>
</div>
<!-- 文件导入end -->
<%}%>
<script type="text/javascript">
function gotoJumpPage(){
	var jumpNum= $("#jumpNum").val();
	$("#search_page_size").val($("#id_page_size").val());
	gotoPage(jumpNum);
	return false;
}
<!--
$(function(){

	$("#searchData").click(function(){
		doSubmit();
	});

<%if (incEditTable){%>

		<c:forEach items="${beans}" var="o">
		<c:if test="${o.dataType=='date'}">
			$("#deal${o.fieldId}").datepicker({ dateFormat: 'yy-mm-dd' });
		</c:if>			
		</c:forEach>


				/** 添加 **/	 
	$("#addData").click(function(){
		//tips.text("").removeClass("ui-state-highlight");
		doReset(document.editFm);
		$("#editDIV").dialog("open");
	});

				/** 编辑 **/
	$(".editData").click(function(){
	    
		var id = $(this).attr("id");
		var editURL =myEditUrl + id;
		$.ajax({
			url:editURL,
			type: 'POST',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				 $.dialog.alert({title:"保存",content:"加载数据异常。"});
			},
			success: function(data){
				fillDIV(data);
				
				$("#dealagencyid").val(data.id);
			}
		});
		$("#editDIV").dialog("open");
	});
			/** 添加用户 **/
	$(".addUserData").click(function(){
	    
		var id = $(this).attr("id");
		var editURL =myEditUrl + id;
		$.ajax({
			url:editURL,
			type: 'POST',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				 $.dialog.alert({title:"保存",content:"加载数据异常。"});
			},
			success: function(data){
				//fillDIV(data);
				//alert(data.agencyName);
				$("#dealuseragencyName").val(data.agencyName);
			}
		});
		$("#editUserDIV").dialog("open");
	});

$("#editUserDIV").dialog({
		autoOpen : false,
		bgiframe: true, 
		height : 350,
		width : 450,
		modal : true,
		resizable :false,
       hide: '',
        show:'',
        zIndex:1000,
		buttons : {
			"保 存" : function() {
				var bValid = doValidUser();

				if(bValid){
					
					var url = mySaveUserUrl;
					
					$.ajax({
						url:url,
						type: 'POST',
						data: $("#editUserFm").serialize(),
							
						error: function(XMLHttpRequest, textStatus, errorThrown){alert('保存异常。' + textStatus + "/" + errorThrown);},
						success: function(data){
						  
						    if(data.result==1) {
								$("#editUserDIV").dialog("close");
								
								query();
							} else {
								//alert("保存失败："+data.errorMessage);
								 $.dialog.alert({title:"保存",content:"保存失败："+data.errorMessage});
							}
						}
					});  
				}
			},
			"取 消" : function() {
				$(this).dialog("close");			
			}
		},
		close : function() {
			//allFields.val("").removeClass("ui-state-error");
		}
	});
	
	$("#editDIV").dialog({
		autoOpen : false,
		bgiframe: true, 
		height : 350,
		width : 450,
		modal : true,
		resizable :false,
       hide: '',
        show:'',
        zIndex:1000,
		buttons : {
			"保 存" : function() {
				var bValid = doValid();
                
				if(bValid){
					var isAdd = $("#dealid").val() == null || $("#dealid").val() == "0" ;
					var url = isAdd?myAddUrl:mySaveUrl;
					
					$.ajax({
						url:url,
						type: 'POST',
						data: $("#editFm").serialize(),
							
						error: function(XMLHttpRequest, textStatus, errorThrown){
						//alert('保存异常。' + textStatus + "/" + errorThrown);
						 $.dialog.alert({title:"保存",content:"保存异常。"});
						},
						success: function(data){
							if(data.result==1) {
								$("#editDIV").dialog("close");
								if (isAdd) {
									//alert('添加成功.');
									$.dialog.alert({title:"保存",content:"添加成功"});
								}
								else {
									//alert('修改成功.');
									$.dialog.alert({title:"保存",content:"修改成功"});
								}
								query();
							} else {
								
									$.dialog.alert({title:"保存",content:"保存失败："+data.errorMessage});
								
							}
						}
					});  
				}
			},
			"取 消" : function() {
				$(this).dialog("close");			
			}
		},
		close : function() {
			//allFields.val("").removeClass("ui-state-error");
		}
	});

function doValidUser(){
		<c:forEach items="${beans1}" var="o">
			<c:if test="${!o.nullable}">
			var obj = $("#deal${o.fieldId}");
			if (isNull(obj.val()))
			{
				//alert('${o.title}不能为空！');
				$.dialog.alert({title:"保存",content:"${o.title}不能为空！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}
			</c:if>	
		<c:if test="${o.dataType=='int'}">
			<c:if test="${o.nullable}">
			var obj = $("#deal${o.fieldId}");
			</c:if>	
			if (!isInt(obj.val()))
			{
				//alert('${o.title}须为整数！');
				$.dialog.alert({title:"保存",content:"${o.title}须为整数！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}			
		</c:if>			
		</c:forEach>
		return true;
	}

	function doValid(){
		<c:forEach items="${beans}" var="o">
			<c:if test="${!o.nullable}">
			var obj = $("#deal${o.fieldId}");
			if (isNull(obj.val()))
			{
				//alert('${o.title}不能为空！');
				$.dialog.alert({title:"保存",content:"${o.title}不能为空！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}
			</c:if>	
		<c:if test="${o.dataType=='int'}">
			<c:if test="${o.nullable}">
			var obj = $("#deal${o.fieldId}");
			</c:if>	
			if (!isInt(obj.val()))
			{
				//alert('${o.title}须为整数！');
				$.dialog.alert({title:"保存",content:"${o.title}须为整数！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}			
		</c:if>			
		</c:forEach>
		return true;
	}

<%}%>

<%if (incDisplayTable){%>
		/** 导入功能 **/	 
	$("#importData").click(function(){
		//$("#formFile").val("").removeClass("ui-state-error");
		$("#ImportDataDiv").dialog("open");
	});


		$("#ImportDataDiv").dialog({
		autoOpen : false,
		height : 200,
		width : 450,
		modal : true,
		buttons : {
			"提 交" : function() {
				var filepath = document.getElementById("formFile").value;
				if(filepath==null || filepath=="") {
					//alert("请添加xls文件！");
					$.dialog.alert({title:"保存",content:"请添加xls文件！"});
					return ;
				}
				var re_text = /\xls/i;
				var newFileName = filepath.split('.');
				newFileName = newFileName[newFileName.length - 1];
				
				if (newFileName.search(re_text) == -1) {
					//alert("您上传的文件格式错误！请上传文件格式为xls/xlsx！");
					$.dialog.alert({title:"保存",content:"您上传的文件格式错误！请上传文件格式为xls/xlsx！"});
					return;
				}
				$.ajaxFileUpload
				(
					{
						url:myUploadUrl, 
						type: 'post',
						secureuri:false,
						fileElementId:'formFile',
						dataType: 'JSON',
						success: function (data, status)
						{     
							var s = eval("("+data+")");
							if (s.result==1) {
								$("#ImportDataDiv").dialog("close");
								//alert("导入成功！");
								$.dialog.alert({title:"保存",content:"导入成功！"});
								query();
							}
							else {
								//alert(""+s.errorMessage);
								$.dialog.alert({title:"保存",content:"导入失败"+s.errorMessage});
							}
						},
						error: function (data, status, e)
						{
							//alert("e:"+e);
							$.dialog.alert({title:"保存",content:"导入失败"+e});
						}
					}
				);
			},
			"取 消" : function() {
				$(this).dialog("close");
			}
		},
		close : function() {
			$("#formFile").val("").removeClass("ui-state-error");
		}
	});
<%}%>

  
	/** 删除 **/
	$(".deleteData").click(function(){
		
		 var id = $(this).attr("id");
		 $.dialog.confirm({title:"删除",content:"您确定要删除吗",callback:"deleteData("+id+");"});
		
		
	});

<%if (incBatchDelete){%>
		$("#batchDeleteButton").click(function(){
		
		 $.dialog.confirm({title:"删除",content:"确实要删除所选记录吗？",callback:"deleteBatchData();"});
		
	});
<%}%>
<c:if test="${not empty outputData }">
	$("#outputDatas").click(function(){
		var f= document.searchForm;
		f.action = "${pageContext.request.contextPath }/backend/report/${outputData}/output";
		f.target="_blank";
		f.submit();
		f.target="_self";
		f.action = "${pageContext.request.contextPath }/backend/${outputData}/search";
		}
	)
	</c:if>
	<c:if test="${not empty incCopyAll &&  incCopyAll =='true'}">
	$(".copyData").click(function(){
		var id = $(this).attr("id");
		var URL = copyUrl+id;
		$.ajax({
			url: URL,
			type: 'POST',

			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				$.dialog.alert({title:"复制",content:"加载数据异常。"});
			},
							
			error: function(XMLHttpRequest, textStatus, errorThrown){
			 // alert('操作异常。' + textStatus + "/" + errorThrown);
			  $.dialog.alert({title:"复制",content:"操作异常。"});
			},
			success: function(data){
				if(data.result==1) {								
					//alert('复制成功.');
					$.dialog.alert({title:"复制",content:"复制成功"});
					query();
				} else {
					//alert("复制失败."+data.errorMessage);
					$.dialog.alert({title:"复制",content:"复制失败"+data.errorMessage});
					return;
				}
			}
		}); 
	});	
</c:if>
});
 function deleteData(id){
   var deleteURL = mydeleteUrl + id;
     $.ajax({
			url:deleteURL,
			type: 'POST',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				$.dialog.alert({title:"删除",content:"加载数据异常"});
			},
			success: function(data){
				if(data.result==1) {
					//alert("删除成功");
					$.dialog.alert({title:"删除",content:"删除成功"});
					/** 删除后触发查询 **/
					query();
				} else {
					//alert("删除失败"+data.errorMessage);
					$.dialog.alert({title:"删除",content:"删除失败"+data.errorMessage});
				}
			}
		});
   }
	function deleteBatchData(){
       
		$.ajax({
			url:myBatchDelUrl,
			type: 'POST',
				data: $("#deleteForm").serialize(),
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				$.dialog.alert({title:"删除",content:"加载数据异常"});
			},
							
						error: function(XMLHttpRequest, textStatus, errorThrown){alert('操作异常。' + textStatus + "/" + errorThrown);},
						success: function(data){
							if(data.result==1) {								
								//alert('删除成功.');
								$.dialog.alert({title:"删除",content:"删除成功"});
								query();
							} else {
								//alert("删除失败."+data.errorMessage);
								$.dialog.alert({title:"删除",content:"删除失败"+data.errorMessage});
								return;
							}
						}
					});  

					return false;
}
//-->
</script>